package br.com.po1.aula2;

import java.text.DecimalFormat;

import javax.swing.JOptionPane;

public class Triangulo {
	private float base;
	private float altura;
	
	public void ler(){
		//base e altura
		do{
			setBase(Float.parseFloat(JOptionPane.showInputDialog(null, "Informe a base", "Calculo da Area", JOptionPane.QUESTION_MESSAGE)));
			if(getBase() <=0) JOptionPane.showMessageDialog(null, "Valor de base invalido, digite novamente!", "Error", JOptionPane.WARNING_MESSAGE);
		}while(getBase() <= 0);
		do{
			setAltura(Float.parseFloat(JOptionPane.showInputDialog(null, "Informe a altura", "Calculo da Area", JOptionPane.QUESTION_MESSAGE)));
			if(getBase() <=0) JOptionPane.showMessageDialog(null, "Valor de altura invalido, digite novamente!", "Error", JOptionPane.WARNING_MESSAGE);
		}while(getAltura() <= 0);
	}
	public void mostrar(){
		//base, altura e area
		StringBuilder triangulo = new StringBuilder();
		triangulo.append("Base: ").append(getBase());
		triangulo.append("\nAltura: ").append(getAltura());
		triangulo.append("\nArea: ").append(formatar(calcular()));
		JOptionPane.showMessageDialog(null, triangulo.toString(), "Area do Triangulo", JOptionPane.INFORMATION_MESSAGE);
	}
	public float calcular(){
		return (base * altura) / 2f;
	}
	public float getBase() {
		return base;
	}
	public void setBase(float base) {
		this.base = base;
	}
	public float getAltura() {
		return altura;
	}
	public void setAltura(float altura) {
		this.altura = altura;
	}
	
	public String formatar(float valor){
		DecimalFormat df = new DecimalFormat("#.00");
		return df.format(valor);
	}
	

}
